using System.Linq;
using Magiq.Delete;
using Magiq.Tests.Support;
using Magiq.Tests.TestModel;
using NUnit.Framework;

namespace Magiq.Tests.MagiqToObjects {
  [TestFixture]
  public class EnumerableSourceFinderTests {
    private readonly TestDomain domain = new TestDomain();

    [Test]
    public void ShouldProvideFromQueryable() {
      var items = domain.Class1s.AsQueryable();
      var source = new EnumerableSourceFinder().FindSourceOf(items);
      source.Should(Be.EqualTo(domain.Class1s));
    }

    [Test]
    public void ShouldProvideFromSingleList() {
      var items = domain.Class1s;
      var source = new EnumerableSourceFinder().FindSourceOf(items);
      source.Should(Be.EqualTo(domain.Class1s));
    }

    [Test]
    public void ShouldProvideFromWhere() {
      var items = domain.Class1s.Where(x => x.StringProperty == "sarasa");
      var source = new EnumerableSourceFinder().FindSourceOf(items);
      source.Should(Be.EqualTo(domain.Class1s));
    }

    [Test]
    public void ShouldProvideFromWhereQueryable() {
      var items = domain.Class1s.AsQueryable().Where(x => x.StringProperty == "sarasa");
      var source = new EnumerableSourceFinder().FindSourceOf(items);
      source.Should(Be.EqualTo(domain.Class1s));
    }

    [Test]
    public void ShouldProvideFromWhereWithFilterChain() {
      var items = domain.Class1s.Where(x => x.StringProperty == "sarasa").Where(x => x.IntProperty == 4);
      var source = new EnumerableSourceFinder().FindSourceOf(items);
      source.Should(Be.EqualTo(domain.Class1s));
    }

    [Test]
    public void ShouldProvideFromWhereWithFilterChainQueryable() {
      var items = domain.Class1s.AsQueryable().Where(x => x.StringProperty == "sarasa").Where(x => x.IntProperty == 4);
      var source = new EnumerableSourceFinder().FindSourceOf(items);
      source.Should(Be.EqualTo(domain.Class1s));
    }

    [Test]
    [ExpectedException(typeof (SourceIsFixedSizeException))]
    public void ShouldThrowSourceIsFixedSizeExceptionIfSourceIsAnArray() {
      var items = domain.Class1s.ToArray();
      new EnumerableSourceFinder().FindSourceOf(items);
    }

    [Test]
    [ExpectedException(typeof (SourceIsFixedSizeException))]
    public void ShouldThrowSourceIsFixedSizeExceptionIfSourceIsAReadOnlyList() {
      var items = domain.Class1s.AsReadOnly();
      new EnumerableSourceFinder().FindSourceOf(items);
    }
  }
}